import psycopg2

conn = psycopg2.connect(database="demo", user="odooe", password="123", host="127.0.0.1", port="5432"
            ,keepalives=1,
            keepalives_idle=30,
            keepalives_interval=10,
            keepalives_count=5)
def insert(sql):
    try:
        cur = conn.cursor()
        print(sql)
        cur.execute(sql)
        conn.commit()
        print('新增成功')
    except Exception as e:
        print(e)
        conn.rollback()

def query(sql):
    try:
        cur = conn.cursor()
        print(sql)
        cur.execute(sql)
        rows = cur.fetchall()
        conn.commit()
        return rows
    except Exception as e:
        print(e)
        conn.rollback()

def update(sql):
    try:
        cur = conn.cursor()
        print(sql)
        cur.execute(sql)
        conn.commit()
        print('更新成功')
    except Exception as e:
        print(e)
        conn.rollback()
